package com.clouddot.baipai.infrastructure.environment.initial;


import android.content.pm.PackageManager;

import com.clouddot.baipai.infrastructure.environment.Env;
import com.clouddot.baipai.infrastructure.environment.Environment;
import com.clouddot.baipai.ui.util.ThreadPool.ThreadPoolFactory;
import com.clouddot.baipai.model.beam.getVersionInfo.XMLBean;


public class InitialAppUpdate extends InitialBase
{
	private Environment Environment;

	public InitialAppUpdate(Environment Environment)
	{
		super(Environment);
		this.Environment = Environment;
	}

	@Override
	public String getName()
	{
		return "初始化升级类";
	}

	@Override
	public void initial() throws Exception
	{
		super.initial();
		ThreadPoolFactory.getThread().execute(new Runnable() {
			@Override
			public void run() {
				try
				{
					final XMLBean xmlBean = Environment.ftpUtil.getVersionCode("/Banpai/version.xml");
					if(xmlBean != null) {
						final PackageManager manager = Environment.context.getPackageManager();
						int versionCode = manager.getPackageInfo(Environment.context.getPackageName(), 0).versionCode;
						if (versionCode < Integer.parseInt(xmlBean.getVersion())) {
							Environment.appContext.upateUrl = xmlBean.getUrl();
							Environment.appContext.updateTip = xmlBean.getData();
							Environment.appContext.updateVer = xmlBean.getVersion();
							Environment.appContext.isUpdata = true;

						} else {
							Environment.appContext.isUpdata = false;
						}
						Environment.appContext.isCancleUpdate = true;
					}
				}
				catch (Exception e)
				{
					Env.error(Environment.fileUtil.Errorinfo(e));

				}
			}
		});



	}

	@Override
	public void reinitial() throws Exception
	{
		super.reinitial();

		ThreadPoolFactory.getThread().execute(new Runnable() {
			@Override
			public void run() {
				try
				{
					final XMLBean xmlBean = Environment.ftpUtil.getVersionCode("/Banpai/version.xml");
					if(xmlBean != null) {
						final PackageManager manager = Environment.context.getPackageManager();
						int versionCode = manager.getPackageInfo(Environment.context.getPackageName(), 0).versionCode;
						if (versionCode < Integer.parseInt(xmlBean.getVersion())) {
							Environment.appContext.upateUrl = xmlBean.getUrl();
							Environment.appContext.updateTip = xmlBean.getData();
							Environment.appContext.updateVer = xmlBean.getVersion();
							Environment.appContext.isUpdata = true;

						} else {
							Environment.appContext.isUpdata = false;
						}
						Environment.appContext.isCancleUpdate = true;
					}
				}
				catch (Exception e)
				{
					Env.error(Environment.fileUtil.Errorinfo(e));

				}
			}
		});
	}
}
